﻿using System.Collections.Generic;
using UnityEngine;

namespace HEFramework
{
    public static class LegacyLotterySystem
    {
        [EntitySystem]
        public class LegacyLotteryAwakeSystem : AwakeSystem<LegacyLottery>
        {
            protected override void Awake(LegacyLottery _self)
            {
                var dc = _self.GetSceneChild<DataTableComponent>();
                if (dc == null)
                {
                    return;
                }

                _self.LotteryCount = 0;
            }
        }


        public static bool Lottery(this LegacyLottery _self)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            var legacy = _self.GetSceneChild<Legacy>();
            var player = _self.GetSceneChild<Player>();
            if (dc == null || legacy == null || player == null)
            {
                return false;
            }

            if (!_self.IsCanLottery())
            {
                return false;
            }

            _self.Lotteries.Clear();

            int max = 3;
            int count = 0;

            if (legacy.LegacyFinds.Count <= max)
            {
                _self.Lotteries.AddRange(legacy.LegacyFinds);
            }
            else
            {
                while (true)
                {
                    if (count == max)
                    {
                        break;
                    }

                    var id = legacy.LegacyFinds[Random.Range(0, legacy.LegacyFinds.Count)];
                    if (!_self.Lotteries.Contains(id))
                    {
                        count++;
                        _self.Lotteries.Add(id);
                    }
                }
            }

            return true;
        }

        public static void Select(this LegacyLottery _self, int _id)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            var legacy = _self.GetSceneChild<Legacy>();
            if (dc == null || legacy == null)
            {
                return;
            }

            if (_id == -1)
            {
                return;
            }

            if (_self.Lotteries.Count <= 0)
            {
                return;
            }

            if (!_self.Lotteries.Contains(_id))
            {
                return;
            }

            //抽奖次数减少
            _self.LotteryCount--;
            //清空挑选池子
            _self.Lotteries.Clear();
            //添加发现的遗物
            legacy.AddFindLegacy(_id);
        }

        public static void Abandon(this LegacyLottery _self)
        {
            //抽奖次数减少
            _self.LotteryCount--;
            //清空挑选池子
            _self.Lotteries.Clear();
        }


        /// <summary>
        /// 存在已抽取物
        /// </summary>
        /// <param name="_self"></param>
        /// <returns></returns>
        public static bool IsHaveCache(this LegacyLottery _self)
        {
            return _self.Lotteries.Count > 0;
        }

        /// <summary>
        /// 是否可抽
        /// </summary>
        /// <param name="_self"></param>
        /// <returns></returns>
        public static bool IsCanLottery(this LegacyLottery _self)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            var legacy = _self.GetSceneChild<Legacy>();
            var player = _self.GetSceneChild<Player>();
            if (dc == null || legacy == null || player == null)
            {
                return false;
            }

            if (legacy.LegacyFinds.Count <= 0)
            {
                return false;
            }

            if (_self.LotteryCount <= 0)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 是否可选
        /// </summary>
        /// <param name="_self"></param>
        /// <returns></returns>
        public static bool IsCanSelect(this LegacyLottery _self, int _id)
        {
            var legacy = _self.GetSceneChild<Legacy>();
            if (legacy == null)
            {
                return false;
            }

            return !legacy.IsMax(_id);
        }
    }
}